/*
 *所有关于BOM类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*BOMService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using System.Linq.Expressions;
using VOL.Core.BaseProvider;
using VOL.Core.Extensions;
using VOL.Core.Extensions.AutofacManager;
using VOL.Core.Utilities;
using VOL.Entity.DomainModels;
using VOL.Smartmanufacturing.DTO.Bom;
using VOL.Smartmanufacturing.Infrastructure;
using VOL.Smartmanufacturing.IRepositories;

namespace VOL.Smartmanufacturing.Services
{
    public partial class BOMService
    {
        /// <summary>
        /// bom仓储
        /// </summary>
        private readonly IBaseRepository<BOM> _bomReppository;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="bomReppository"></param>
        public BOMService(IBaseRepository<BOM> bomReppository)
        {
            _bomReppository = bomReppository;
        }
        /// <summary>
        /// 显示bom数据
        /// </summary>
        /// <param name="getBomDto">查询分页参数</param>
        /// <returns></returns>
        public async Task<ApiPaging<BOM>> GetBom(GetBomDto getBomDto)
        {
            ApiPaging<BOM> res = new ApiPaging<BOM>();

            var boms = _bomReppository.GetAll();
            //按照bom编号模糊查询
            boms = boms.WhereIF(!string.IsNullOrEmpty(getBomDto.BomCode),a=>a.BomCode.Contains(getBomDto.BomCode));
            //按照物料编号模糊查询
            boms = boms.WhereIF(!string.IsNullOrEmpty(getBomDto.MaterialNumber), a => a.MaterialNumber.Contains(getBomDto.MaterialNumber));

            res.TotalCount= boms.Count();
            res.TotalPage = (int)Math.Ceiling(boms.Count()*1.0/ getBomDto.PageSize);
            res.Items= boms.OrderByDescending(a=>a.Id).Skip((getBomDto.PageIndex-1)* getBomDto.PageSize).Take(getBomDto.PageSize).ToList();
            return await Task.FromResult(res);
        }

        /// <summary>
        /// 添加Bom
        /// </summary>
        /// <param name="bom"></param>
        /// <returns></returns>
        public async Task<int> AddBom(BOM bom) 
        {
        var result=await _bomReppository.AddAsync(bom);
            return await Task.FromResult(result);
        }

        /// <summary>
        /// 删除Bom
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> DelBom(string ids) 
        {
        var arrid= ids.Split(',');
            var result = 0;
            foreach (var id in arrid) 
            {
                result=await _bomReppository.DelAll(a=>a.Id==Convert.ToInt32(id));

            }
            return await Task.FromResult(result);   
        }
        /// <summary>
        /// 修改bom数据
        /// </summary>
        /// <param name="bom">bom实体</param>
        /// <returns></returns>
        public async Task<int> UpDateBom(BOM bom) 
        {
            var result=await _bomReppository.UpdateAsync(bom);
            return await Task.FromResult(result);   
        }



    }
}
